//
//  imessageLib.m
//  imessageLib
//
//  Created by romejiang on 14-2-21.
//  Copyright (c) 2014年 com.romejiang. All rights reserved.
//

#import "imessageLib.h"
#import "xpc.h"


@implementation imessageLib

- (BOOL)sendMessage:(NSString *) appleid
                msg:(NSString *)msg{

//    NSString* recipient = @"18691915676";
//    NSString* text = @"Hello world from romejiang";
    
    dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc_connection_queue", DISPATCH_QUEUE_SERIAL);
    xpc_connection_t connection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, 0);
    xpc_connection_set_event_handler(connection, ^(xpc_object_t object){ });
    xpc_connection_resume(connection);
    dispatch_release(queue);
    
    xpc_object_t dictionary = xpc_dictionary_create(0, 0, 0);
    xpc_dictionary_set_int64(dictionary, "message-type", 0);
    NSData *data = [NSPropertyListSerialization dataWithPropertyList:[NSArray arrayWithObject:appleid] format:NSPropertyListBinaryFormat_v1_0 options:0 error:NULL];
    xpc_dictionary_set_data(dictionary, "recipients", data.bytes, data.length);
    xpc_dictionary_set_string(dictionary, "markup", [msg UTF8String]);
    //    xpc_dictionary_set_bool(dictionary, "forceSMS", true);
    
    xpc_connection_send_message(connection, dictionary);
    //    xpc_connection_send_barrier(connection, ^{
    NSLog(@"Message has been successfully delievered");
    //    });
    xpc_release(dictionary);
    
    return TRUE;
}

@end
